#import "UmflutterPlugin.h"
#import <UMCommon/UMCommon.h>
#import <UMCommon/MobClick.h>

@implementation UmflutterPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"umflutter"
            binaryMessenger:[registrar messenger]];
  UmflutterPlugin* instance = [[UmflutterPlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"init" isEqualToString:call.method]) {
    [UMConfigure initWithAppkey:call.arguments[@"appKey"] channel:call.arguments[@"channel"]];
    result([NSNumber numberWithBool:true]);
  } else if([@"signIn" isEqualToString:call.method]) {
    [MobClick profileSignInWithPUID:call.arguments];
    result([NSNumber numberWithBool:true]);
  }else if([@"signOff" isEqualToString:call.method]) {
    [MobClick profileSignOff];
    result([NSNumber numberWithBool:true]);
  }else if([@"pageStart" isEqualToString:call.method]) {
    [MobClick beginLogPageView:call.arguments]; //("Pagename"为页面名称，可自定义)
    result([NSNumber numberWithBool:true]);
  }else if([@"pageEnd" isEqualToString:call.method]) {
    [MobClick endLogPageView:call.arguments]; //("Pagename"为页面名称，可自定义)
    result([NSNumber numberWithBool:true]);
  }else if([@"event" isEqualToString:call.method]) {
    NSString *event = call.arguments[@"event"];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:0];
    for(NSString *key in call.arguments){
        if([@"event" isEqualToString:key]){
            continue;
        } else{
            [dic setObject:[call.arguments objectForKey:key] forKey:key];
        }
    }
    [MobClick event:event attributes:dic];
    result([NSNumber numberWithBool:true]);
  }else {
    result(FlutterMethodNotImplemented);
  }
}

@end
